<?php

namespace App\Jobs;

use App\Models\ImGroup;
use App\Models\ImGroupRelate;
use App\Models\ImQueueErrorLogs;
use App\Services\Huanxin\GroupServices;
use App\Services\Huanxin\UserServices;
use App\Services\RongCloud\GroupService;
use App\Services\Netease\GroupServices as NetGroupServices;
use App\Services\RongCloud\UserService as RongUserService;
use App\Services\Netease\UserServices as NeteaseUserService;
use App\Services\Aliyun\Video\VideoServices;
use Gregwar\Captcha\CaptchaBuilder;
use Gregwar\Captcha\PhraseBuilder;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

class GroupUserTask implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    private $data = null;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($data = null)
    {
        //
        $this->data = $data;
    }

    /**
     * @desc 同步群成员用户
     * @return bool
     * @author zhouzhou
     * @time 2021/12/24 16:30
     */
    public function handle()
    {
        // redis的数据
        $data = $this->data;
        if (!empty($data))
        {
            try{
                $imGroup = new ImGroup();
                $imGroupRelate = new ImGroupRelate();


                $groupId = $data['group_id'];
                $member = $data['member_id'];
                $groupName = $data['group_name'];
                $userId = $data['user_id'];
                $type = $data['type']; // 1环信 2融云 3网易云
                $imGroupInfo = $imGroup->where(['im_gid' => $groupId])->first();
                $memberList = !empty($member) ?  explode(",",$member) : [];

                // 同步融云 网易云
                if ($type == 1)
                {
                    $imRongRelate = $imGroupRelate->where('im_gid', $groupId)->where('type', 2)->first();

                    // 同步融云
                    $groupRongServices= new GroupService();
                    if ($memberList && !empty($imRongRelate))
                    {
                        $groupRongServices->joinGroup($memberList, $imRongRelate['relate_gid'], $groupName);
                    }

                    // 同步网易云
                    $imNetRelate = $imGroupRelate->where('im_gid', $groupId)->where('type', 3)->first();

                    $groupNetServices= new NetGroupServices();
                    if ($memberList && !empty($imNetRelate))
                    {
                        $groupNetServices->addIntoGroup($imNetRelate['relate_gid'],$userId, $memberList);
                    }
                }

                // 同步im和网易云
                if ($type == 2)
                {
                    // 同步im群组
                    $imRelate = $imGroupRelate->where('im_gid', $groupId)->where('type', 1)->first();
                    $groupServices= new GroupServices();
                    if ($memberList && !empty($imRelate))
                    {
                        if (count($memberList) > 1)
                        {
                            $service['usernames'] = $memberList;
                            $groupServices->addChatGroupsMembers($imRelate['relate_gid'], $service);
                        }
                        else
                        {
                            $groupServices->addChatGroupsMember($imRelate['relate_gid'], $member);
                        }
                    }

                    // 同步网易云
                    $imNetRelate = $imGroupRelate->where('im_gid', $groupId)->where('type', 3)->first();

                    $groupNetServices= new NetGroupServices();
                    if ($memberList && !empty($imNetRelate))
                    {
                        $groupNetServices->addIntoGroup($imNetRelate['relate_gid'],$userId, $memberList);
                    }


                }

                // 同步im和融云
                if ($type == 3)
                {
                    // 同步im群组
                    $imRelate = $imGroupRelate->where('im_gid', $groupId)->where('type', 1)->first();
                    $groupServices= new GroupServices();
                    if ($memberList && !empty($imRelate))
                    {
                        if (count($memberList) > 1)
                        {
                            $service['usernames'] = $memberList;
                            $groupServices->addChatGroupsMembers($imRelate['relate_gid'], $service);
                        }
                        else
                        {
                            $groupServices->addChatGroupsMember($imRelate['relate_gid'], $member);
                        }
                    }

                    $imRongRelate = $imGroupRelate->where('im_gid', $groupId)->where('type', 2)->first();

                    // 同步融云
                    $groupRongServices= new GroupService();
                    if ($memberList && !empty($imRongRelate))
                    {
                        $groupRongServices->joinGroup($memberList, $imRongRelate['relate_gid'], $groupName);
                    }

                }
            }
            catch (\Exception $e)
            {
                $imQueueErrorLogs = new ImQueueErrorLogs();
                $imQueueErrorLogs->insertQueueError(json_encode(['data'=>$data]), 3, $e->getMessage());
            }

        }

    }
}
